package com.chenye.service;

import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.sleuth.Span;
import org.springframework.cloud.sleuth.Tracer;
import org.springframework.cloud.sleuth.annotation.NewSpan;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @Auther:chenye
 * @Data:2018/6/25
 * @Description:
 */
@SpringBootApplication
@RestController
@EnableTransactionManagement
public class ServiceApplication {
    public static void main(String[] args){
        ApplicationContext a = SpringApplication.run(ServiceApplication.class,args);
        Person person = a.getBean("person",Person.class);
        person.getPerson();

        Create create = a.getBean("create",Create.class);

    }
    private static final Logger log = LoggerFactory.getLogger(ServiceApplication.class);


    @Autowired
    private RestTemplate restTemplate;

    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }



    @Autowired
    private Tracer tracer;

    @RequestMapping("hi")
//    @NewSpan("service span")
    public String sayHi(){
        log.info("service info");
        Span span = tracer.createSpan("tracer.createSpan",tracer.getCurrentSpan());
        tracer.addTag("service 的 tag","value");

        tracer.addTag("this","begin");
        rt.setExchange("test.zipkin");
        rt.convertAndSend("test.zipkin", "xixi");
        tracer.addTag("this1","end");
        tracer.close(span);
        return restTemplate.getForObject("http://localhost:8082/getHi",String.class);
//        tracer.close(span);
//        return "xixi";
    }

    @RequestMapping("callBack")
    public String callBack(){
        tracer.addTag("callBack","callBack");
        return "callBack";
    }


    @Autowired
    private RabbitTemplate rt;

    @RequestMapping("sendMq")
//    @NewSpan(value = "send_Mq_Span")
    public void sendMq(){
        Span span = tracer.getCurrentSpan();
        Span newSpan = tracer.createSpan("入口Span", span);
        newSpan.tag("123132","123123");
        tracer.addTag("this","begin");
        tracer.addTag("this1","end");
        tracer.close(newSpan);
        rt.setExchange("test.zipkin");
        rt.convertAndSend("test.zipkin", "xixi");
    }
}
